package kr.totalcall.view.receive
{
	/**
	 * 작업 기록
	 * 2012. 7. 2. / 이규광 / 최초 작성.
	 */
	/**
	 * 내용.
	 * @productversion Flex 4.5
	 * @langversion ActionScript 3.0
	 * @playerversion Flash 10.2
	 * @author 이규광.
	 */
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	
	import kr.totalcall.constant.TotalCallGlobals;
	import kr.totalcall.events.ItemEvent;
	import kr.totalcall.service.HttpService;
	
	import mx.collections.ArrayCollection;
	import mx.collections.IList;
	import mx.events.CloseEvent;
	
	public class CallPassReceiveController extends EventDispatcher
	{
		
		public var sound_dingdong:Sound = new Sound(new URLRequest("./snd/dingdong.mp3"));
		public var soundChannel_dingdong:SoundChannel;
		
		[Bindable] public var callPassAC:ArrayCollection = new ArrayCollection();
		
		public var windowCallPass:EventDispatcher ;
		
		public var httpCallpassConfirm:HttpService
		
		/**
		 * 생성자
		 */
		public function CallPassReceiveController(target:IEventDispatcher=null)
		{
			super(target);
			httpCallpassConfirm = new HttpService();
			httpCallpassConfirm.url = "callpass_confirm.php";
		}
		
		
		public function reset():void{
			callPassAC.removeAll();
		}
		
		
		/**
		 * list에 있는 아이템을 모두 체크 
		 * 존재하지 않으면 add
		 * 존재하지만 콜패스가 아니면 remove
		 */
		public function checkAndAddRemoveAll(list:XMLList):void{
			if(!list) {
				return;
			}
			var newItemExist:Boolean = false;
			
			callPassAC.removeAll();
			
			var i : int = 0;
			for(i = 0; i < list.length(); i++){
				if(list[i].status == "콜패스" && list[i].callpass_confirm_yn == "N" ){
					if(list[i].branch_num == TotalCallGlobals.branchNum ){
						if(!existItem(list[i])){
							callPassAC.addItem(list[i]);
							newItemExist = true;
						}
					}
				} else if(list[i].status == "콜복귀"){		// 콜복귀인 경우..?
					if(list[i].branch_num == TotalCallGlobals.branchNum ){
						if(!existItem(list[i])){
							callPassAC.addItem(list[i]);
							newItemExist = true;
						}
					}
				}
			}
			
			/*	// 앞에서 모두 삭제..
			// 콜패스 했으나 접수등.. 인 아이템은 목록에서 삭제한다.
			for(i = 0; i < list.length(); i++){
				if(list[i].status != "콜패스" && list[i].callpass_yn == "Y"){
					
					for(var j:int = callPassAC.length-1; j >= 0; j--){
						if(list[i].num == callPassAC[j].num){
							callPassAC.removeItemAt(j);
						}
					}
				}
			}
			*/
			if(callPassAC && callPassAC.length > 0 ){
				dispatchEvent(new ItemEvent(ItemEvent.NEW_ITEM, null));
				soundStart();
			}else if(callPassAC.length == 0 ){
				soundStop();
				windowCallPass.dispatchEvent(new CloseEvent(CloseEvent.CLOSE));
			}
			
		}
		
		
		
		/**
		 * 콜패스 알림창 리스트에서 아이템하나를 삭제한다.
		 */
		public function removeCallPassItem(item:Object):void{
			if(existItem(item)){
				for( var i : int = 0; i < callPassAC.length; i++){
					if(callPassAC[i].num == item.num){
						callPassAC.removeItemAt(i);
					}
				}
			}
			callPassAC.refresh();
			if(!callPassAC || callPassAC.length <= 0){
				soundStop();
				windowCallPass.dispatchEvent(new CloseEvent(CloseEvent.CLOSE));
			}
		}
		
		
		
		/**
		 * 콜패스 아이템을 확인
		 */
		public function confirmCallPassItem(item:Object):void{
			if(!item  || item.callpass_yn != "Y" ) return;
			if(TotalCallGlobals.userBranchNum != item.branch_num) return;
			
			//trace("(TotalCallGlobals.userBranchNum != item.callpass_branch) :" + (TotalCallGlobals.userBranchNum != item.branch_num) );
			
			var uv:URLVariables = new URLVariables();
			uv.num = item.num;
			httpCallpassConfirm.send(uv);
			removeCallPassItem(item);
		}
		
		
		
		
		/**
		 * 기존에 존재하는 접수인지
		 */
		public function existItem(item:Object):Boolean{
			for( var i : int = 0; i < callPassAC.length; i++){
				if(callPassAC[i].num == item.num){
					return true;
				}
			}
			return false;
		}
		
		
		/**
		 * 소리 시작
		 */
		public function soundStart():void{
			if(TotalCallGlobals.chkCallPassNotice){
				
				if(soundChannel_dingdong){
					//trace(soundChannel_dingdong.position);
					if(soundChannel_dingdong.position > 0){
						return;
					}
					//soundChannel_dingdong.stop();
				}
				soundChannel_dingdong = sound_dingdong.play(0, int.MAX_VALUE);
			}
		}
		
		/**
		 * 소리 멈춤
		 */
		public function soundStop():void{
			if(soundChannel_dingdong){
				soundChannel_dingdong.stop();
			}
			soundChannel_dingdong = null;
		}
	}
}